package com.deltabravomedia.theoldtoad.server;

import java.io.IOException;
import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class CreateItemServlet extends HttpServlet {

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		String type = req.getParameter("type");
		String name = req.getParameter("name");
		String cost = req.getParameter("cost");
		String description = req.getParameter("description");
		String recommend = req.getParameter("recommend");
		
		String redirect = "/addItem.jsp?" + type;			
		if (type.equals("Event")) {
			redirect = "/addEvent.jsp";
		} 

		Item starter = new Item(type, name, cost, description, recommend, true, new Date());
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(starter);
		} finally {
			pm.close();
		}
	
		resp.sendRedirect(redirect);
	}
}
